package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.AppCharmLevelService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppCharmLevel;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 后台 魅力值等级
 */
@Controller
@RequestMapping("/after/charmlevel")
public class AfterCharmLevelController extends BaseController {

    @Autowired
    private AppCharmLevelService appCharmLevelService;

    /**
     * 列表
     *
     * @param appCharmLevel
     * @param splitPage
     * @return
     */
    @RequestMapping("list")
    public String list(AppCharmLevel appCharmLevel, SplitPageBean splitPage) {
        request.setAttribute("list", appCharmLevelService.list(appCharmLevel, splitPage));
        request.setAttribute("splitPage", splitPage);
        request.setAttribute("level", appCharmLevel.getLevel());
        request.setAttribute("surfaceTitle", appCharmLevel.getSurfaceTitle());
        return this.sucess("/web/after/charmlevel/list");
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("toAdd")
    public String toAdd() {
        return this.sucess("/web/after/charmlevel/add");
    }

    /**
     * 跳转到修改页面
     *
     * @return
     */
    @RequestMapping("toUpdate")
    public String toUpdate(Integer id) {
        request.setAttribute("charmlevel", appCharmLevelService.getById(id));
        return this.sucess("/web/after/charmlevel/update");
    }

    /**
     * 添加或修改
     *
     * @param appCharmLevel
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(AppCharmLevel appCharmLevel) {
        if (StringUtils.isEmpty(appCharmLevel.getSurfaceTitle())) {
            return errorJSON("魅力称号不能为空！");
        }
        if (StringUtils.isEmpty(appCharmLevel.getLevel())) {
            return errorJSON("魅力等级不能为空！");
        }
        if (StringUtils.isEmpty(appCharmLevel.getCharmNum())) {
            return errorJSON("升级条件不能为空！");
        }
        appCharmLevelService.saveOrUpdate(appCharmLevel);
        return sucessJSON("操作成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        appCharmLevelService.deleteById(id);
        return sucessJSON("删除成功！");
    }

}
